<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class model_member extends CI_Model{
    /**
     * Initialize table and primary field variable
     */
    var $table      = "tbl_member";
    var $table_ippi = "tbl_member_ippi";
    var $primary    = "id_member";
    
    /**
	* Constructor - Sets up the object properties.
	*/
    public function __construct()
    {
        parent::__construct();
    }
    
    
    /**
     * Retrieve all of member 
     * 
     * @author  Iqbal
     * @return  Object  Result all member data
     */
    function get_all(){
        return $this->db->get($this->table);
    }
    
    /*
     * get all IPPI member
     * 141123
     * 
     */
    function get_all_ippi_member(){
        $this->db->from($this->table_ippi." A");
        $this->db->join($this->table." B", 'A.email = B.email');
        return $this->db->get();
    }
    
    /**
     * Get map grouping of province
     * 
     * @author  Dinov
     * @return  Object  Result map grouping province.
     */
    function map_grouping_provinsi() {
        $q = $this->db->query("
            SELECT P.`id_provinsi`,P.`lat`,P.`long`,P.nama_provinsi,COUNT(*) as jumlah 
            FROM `tbl_kasus` as K 
            LEFT JOIN `tbl_provinsi` as P ON K.`id_provinsi` = P.`id_provinsi` 
            GROUP BY K.`id_provinsi`");
        return $q->result();
    }
    
    /**
     * Save data of member
     * 
     * @author  Dinov
     * @param   Array   $data   (Required)  Array data of member
     * @return  Boolean Boolean false on failed process or invalid data, otherwise true
     */
    function save_data($data){
        if( empty($data) ) return false;
        if( $this->db->insert($this->table,$data) ) return true;
        return false;
    }
    
    /*
     * get data by condition array
     * author dinov
     * 20141127
     */
    function get_by_codition($condition){
        $this->db->where($condition);
        return $this->db->get($this->table);
    }
    /*
     * save ippi member
     * 141127 dinov
     */
    function save_data_ippi_member($data){
        if( empty($data) ) return false;
        if( $this->db->insert($this->table_ippi,$data) ) return true;
        return false;
    }
    
    /**
     * Update data of member
     * 
     * @author  Dinov
     * @param   Int     $id     (Required)  Member ID
     * @param   Array   $data   (Required)  Array data of user
     * @return  Boolean Boolean false on failed process or invalid data, otherwise true
     */
    function update_data($id,$data){
        if(empty($id) || empty($data)) {return false;}
        $this->db->where($this->primary,$id);	
        if($this->db->update($this->table,$data)) {return true;}
        else {return false;}
    }
    
    /**
     * Delete data of member
     * 
     * @author  Dinov
     * @param   Int     $id     (Required)  Member ID
     * @return  Boolean Boolean false on failed process or invalid data, otherwise true
     */
    function delete($id){
        if( empty($id) ) return false;
        if($this->db->delete($this->table, array($this->primary => $id))) return true;
        else return false; 
    }
    
    /**
     * Count member by email
     * 
     * @author  Dinov
     * @param   String  $email  (Required)  Member email
     * @return  Boolean Boolean false on failed process or invalid data, otherwise Int count of member
     */
    function count_by_email($email){
        if( empty($email) ) return false;
        $q = $this->db->query("SELECT * FROM ".$this->table." WHERE email = '".$email."'");
        return $q->num_rows();
    }
    
    /**
     * Count member by password
     * 
     * @author  Dinov
     * @param   String  $email      (Required)  Member email
     * @param   String  $password   (Required)  Member password
     * @return  Boolean Boolean false on failed process or invalid data, otherwise Int count of member
     */
    function count_by_password($email, $password){
        if( empty($email) ) return false;
        if( empty($password) ) return false;
        $q = $this->db->query("SELECT * FROM ".$this->table." WHERE email = '".$email."' AND password = '".$password."'");
        return $q->num_rows();
    }
    
    /**
     * Sign In
     * 
     * Authenticate member and drop login cookie if member is valid.
     * 
     * @author  Iqbal
     * @param   Array   $credential     (Optional)  Associative array of member credential. It contains member_email, member_password, and remember
     * @return  Mixed   False on invalid member, otherwise object of member.
     */
	function signon($credentials)
	{
		if ( empty($credentials) || !is_array($credentials) ) return false;
  
		$member = $this->authenticate( $credentials['email'], $credentials['password'] );
        
		if ( empty($member) ) 
			return false;
        
        if(!empty($member->id_member)) ippi_set_auth_cookie( $member->id_member, false, '' );
		
		return $member;
	}
    
    /**
     * Authenticate member
     * 
     * @author  Iqbal
     * @param   String  $email      (Required)  Email
     * @param   String  $password   (Required)  Password
     * @return  Mixed   False on invalid member, otherwise object of member.
     */
	function authenticate( $email, $password )
	{		
		$email    = sanitize_email($email);
		$password = trim($password);
        
		if ( empty($email) || empty($password) )
			return false;
            
        $memberdata = $this->get_member_by('email', $email);
        
        $logged_in  = check_member_login($memberdata->id_member);
        
        if( !$logged_in )
            return false;
            
        if( $logged_in == 2 )
            return 'logged';
       
        if( !$memberdata )
            return false;

		if( $memberdata && $memberdata->status == 0 )
			return 'not_activated';
        
        if( $memberdata && $memberdata->deleted == 1 )
            return 'deleted';
        
		return $memberdata;
	}
    
    /**
     * Get user data by conditions
     * 
     * @author  Iqbal
     * @param   String  $field  (Required)  Database field name or special field name defined inside this function
     * @param   String  $value  (Optional)  Value of the field being searched
     * @return  Mixed   Boolean false on failed process, invalid data, or data is not found, otherwise StdClass of member
     */
    function get_member_by($field, $value='')
    {
        $id = '';
        
        switch ($field) {
            case 'id':
                $id     = $value;
                break;
            case 'email':
                $value  = sanitize_email($value);
                $id     = '';
                $field  = 'email';
                break;
            default:
                return false;
        }
        
        if ( $id != '' && $id > 0 )
            return $this->get_memberdata($id);
        
        if(empty($field)) return false;
        
        $db     = $this->db;
        $query  = $db->get_where($this->table, array($field => $value));
        
        if ( !$query->num_rows() )
            return false;

        foreach ( $query->result() as $row ) {
            $member = $row;
        }

        return $member;
    }
    
    /**
     * Get member data by email/member ID
     * 
     * @author  Iqbal
     * @param   Integer $member_id  (Required)  Member ID
     * @return  Mixed   False on failed process, otherwise object of member.
     */
    function get_memberdata($member_id){
        if ( !is_numeric($member_id) ) return false;

        $member_id = absint($member_id);
        if ( !$member_id ) return false;
        
        $query = $this->db->get_where($this->table, array($this->primary => $member_id));
        if ( !$query->num_rows() )
            return false;

        foreach ( $query->result() as $row ) {
            $member = $row;
        }
        
        return $member;
    }
    
    /**
     * Get member data by email/member ID
     * 
     * @author  Iqbal
     * @param   Integer $member_id  (Required)  Member ID
     * @param   Int     $mod        (Optional)  Mode of return value    default 'row'
     * @return  Mixed   False on failed process, otherwise object of member.
     */
    function get_memberdata_by_id($member_id, $mod = 'row'){
        if ( !is_numeric($member_id) ) return false;

        $member_id = absint($member_id);
        if ( !$member_id ) return false;
        
        $q = $this->db->query("SELECT * FROM `$this->table` WHERE `".$this->primary."`='".$member_id."'");
        if ( !$query->num_rows() )
            return false;
            
        if(strtolower($mod) == "result") return $q->result();
        
        return $q->row();
    }
    
    /**
     * Hash the password
     * 
     * @author  Iqbal
     * @param   String  $password   (Required)  Password
     * @return  String  Of hashed password
     */
    function hash_password($password)
    {
        if(empty($password)) return false;
        
        $site_key   = $this->config->item('encryption_key');
        $salt       = substr(hash('sha512', $site_key), 0, 8);

        return hash_hmac('sha512', $password . $salt, $site_key);
    }

    /**
     * Hash the password
     * 
     * @author  Dinov
     * @param   Int     $id     (Required)  Member ID
     * @param   String  $mod    (Optional)  Return Model
     * @return  StdClass of member data
     */
    function select_by_id($id, $mod="result"){
        $q = $this->db->query("SELECT * FROM `". $this->table . "` WHERE `".$this->primary."` = '". $id."'");
        if(strtolower($mod) == "result") {return $q->result();}
        if(strtolower($mod) == "count") {return $q->num_rows();}
    }

}
/* End of file model_member.php */
/* Location: ./application/models/model_member.php */
